home *** CD-ROM | disk | FTP | other *** search
- ;; calc.mut : a popup programmers calculator for ME
- ;; C Durland Public Domain
-
- ;; Requires:
- ;; popup.mut
-
- (include me2.h)
- (include tobase.mut)
-
- (int RV TV mem)
- (small-int base)
-
- (defun
- doc HIDDEN ; popup a window with documentation
- {
- (menu-box
- ">Mutt CALC - an RPN calculator"
- "+ - * (or x) /"
- "Enter or Return : move x to total"
- "m : Negate x"
- "| & \^ : bitwise OR, AND, XOR total and x"
- "< > : shift left or right"
- "% : total mod x"
- "s : Store total in memory"
- "r : Recall memory to x"
- "= : Insert total at dot"
- "\^H or BACKSPACE : Erase last digit of x"
- "\^L : Redraw the screen"
- "# : Toggle between decimal and hex"
- "B : Change the radix"
- "k : Put the next key pressed into total"
- 'q ^G : Quit')
- (refresh-screen)
- }
- MAIN
- {
- (base 10) ;; initialize base to decimal
- (require "menu-box" "popup.mut") ;; for (doc)
- }
- inc (int n) HIDDEN ; increment TV by n
- {
- (if (< n base) (TV (+ (* TV base) n)) )
- }
- vert (int n) HIDDEN ; convert n to proper base for display
- {
- (if (== base 10) { n (done) } )
- (if (< n 0) (concat "-" (tobase (- 0 n) base)) (tobase n base))
- }
- ; odd (int n) HIDDEN { (!= n (* (/ n 2) 2)) } ; TRUE if n is odd
- ; bitwise (pointer defun op)(int x y) HIDDEN ; (bitwise-op x y)
- ; {
- ; (int bit result a b)
- ;
- ; (result 0)(bit 1)(a x)(b y)
- ; (while (or (!= 0 a)(!= 0 b))
- ; {
- ; (if (op a b) (+= result bit))
- ; (*= bit 2) ; next bit
- ; (/= a 2)(/= b 2)
- ; })
- ; result
- ; }
- ; bor (int a b) HIDDEN { (or (odd a)(odd b)) } ;TRUE if ((a&1) OR (b&1))==1
- ; band (int a b) HIDDEN { (and (odd a)(odd b)) } ;TRUE if ((a&1) AND (b&1))==1
- ; bxor (int a b) HIDDEN { (odd (+ a b)) } ;TRUE if ((a&1) XOR (b&1))==1
- ; bit-or (int x y) HIDDEN { (bitwise (floc bor) x y) }
- ; bit-and (int x y) HIDDEN { (bitwise (floc band) x y) }
- ; bit-xor (int x y) HIDDEN { (bitwise (floc bxor) x y) }
- calculator
- {
- (int n)
-
- (while TRUE
- {
- (msg "RPN CALC>" base " Memory: " (vert mem base)
- " Total: " (vert RV) " x: " (vert TV) )
- (switch (getchar)
- "0" (inc 0)
- "1" (inc 1)
- "2" (inc 2)
- "3" (inc 3)
- "4" (inc 4)
- "5" (inc 5)
- "6" (inc 6)
- "7" (inc 7)
- "8" (inc 8)
- "9" (inc 9)
- "+" { (+= RV TV)(TV 0) }
- "-" { (-= RV TV)(TV 0) }
- "*" { (*= RV TV)(TV 0) }
- "x" { (*= RV TV)(TV 0) }
- "/" { (if (== 0 TV)(RV 0)(/= RV TV)) (TV 0) }
- "a" (inc 10)
- "b" (inc 11)
- "c" (inc 12)
- "d" (inc 13)
- "e" (inc 14)
- "f" (inc 15)
- "=" { (insert-text (vert RV))(update) }
- "^M" { (RV TV)(TV 0) } ; enter
- "m" (*= TV -1) ; change sign
- "|" { (RV (bit-or RV TV)) (TV 0) }
- "&" { (RV (bit-and RV TV)) (TV 0) }
- '^' { (RV (bit-xor RV TV)) (TV 0) }
- '%' { (if (== 0 TV)(RV 0)(RV (mod RV TV))) (TV 0) }
- "^H" (/= TV base)
- "s" (mem RV) ; store
- "r" (TV mem) ; recall
- "^L" { (refresh-screen)(update) } ; refresh screen
- ">" (/= RV 2) ; shift right
- "<" (*= RV 2) ; shift left
- "#" (if (== base 10)(base 16)(base 10)) ; toggle radix
- "B" ; change radix
- {
- (n (convert-to NUMBER (ask "base = ")))
- (if (and (<= 2 n)(<= n 16)) (base n))
- }
- "k"
- {
- (msg "Press key to convert")
- (RV (convert-to CHARACTER (getchar)))
- }
- "K" { (msg "Press ME key to convert")(RV (get-key)) }
- "?" (doc)
- "q" (break) ; quit
- "^G" (break) ; quit
- )
- })
- }
- )
-